<template>
  <div class="home">
    <el-container>
      <!-- 左边内容 -->
      <el-aside width="auto">
        <div class="logo"></div>
        <!-- default-active 用于设置默认选中的菜单索引 -->
        <!-- collapse属性控制菜单的折叠与展开 -->
        <!-- router属性开启vue-router模式 -->
        <el-menu
          unique-opened
          background-color="#F9F9F9"
          router
          class="el-menu-admin"
          :collapse="iscollapse"
          @open="handleOpen"
          @close="handleClose">
          <el-submenu :index="item.path" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="child.path" v-for="child in item.children" :key="child.id">
              <i class="el-icon-menu"></i>
              <span slot="title">{{child.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>

      <!-- 右边内容 -->
      <el-container>
        <el-header>
          <i class="myicon myicon-menu toggle-btn" @click="toggle"></i>
          <div class="system-title">电商后台管理系统</div>
          <div>
            <span class="welcome">您好，{{username}}</span>
            <el-button type="warning" round @click="handleLogout">退出</el-button>
          </div>
        </el-header>
        <el-main>
          <!-- 用router-view挖坑 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import { getMenus } from '@/api'
export default {
  data () {
    return {
      iscollapse: false,
      username: '',
      menuList: [] // 存储菜单数据
    }
  },
  methods: {
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    // 切换菜单的折叠与展开
    toggle () {
      this.iscollapse = !this.iscollapse
    },
    // 退出登录
    handleLogout () {
      // 清除本地存储
      localStorage.clear()
      // 跳转到登录页
      this.$router.push({ name: 'login' })
    }
  },
  mounted () {
    this.username = localStorage.getItem('username')
    // console.log(this.username)
    getMenus()
      .then(res => {
        this.menuList = res.data
      })
  }
}
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #F9F9F9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409EFF;
  }
  .logo {
    height:60px;
    background: url(../assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292CF;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome, {
    color: white;
  }
}
</style>
